<template>
  <div style="display: flex; justify-content: center; align-items: center;height: 100%;">
    <el-form ref="ruleFormRef" style="margin: 0 auto ; max-width: 600px" :model="ruleForm" :rules="rules"
      label-width="auto" class="demo-ruleForm" :size="formSize" status-icon>
      <el-form-item label="账号" prop="userName">
        <el-input v-model="ruleForm.userName" prefix-icon="UserFilled" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" type=password show-password prefix-icon="Lock" />
      </el-form-item>
      <el-form-item prop="remember">
        <el-checkbox style="margin-left: 50px;" v-model="ruleForm.remember" />&nbsp;记住密码
      </el-form-item>
      <el-form-item>
        <el-button style="margin: 0 auto;" type="primary" @click="submitForm(ruleFormRef)">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import router from "@/router";
import { login, type ApiResponse } from "@/utils/api";
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<ApiResponse>({
  userName: "admin",
  password: '',
  remember: false
})


const rules = reactive<FormRules<ApiResponse>>({
  userName: [
    { required: true, message: 'Please input Activity userName', trigger: 'blur' },
    { min: 1, max: 14, message: 'Length should be 1 to 14', trigger: 'blur' },
  ],
  password: [
    { required: true, message: 'Please input Activity password', trigger: 'blur' },
    { min: 1, max: 24, message: 'Length should be 1 to 24', trigger: 'blur' },
  ],
})
type ApiResponsres = {
  "id": any,
  "userUuid": any,
  "userName": string,  //用户名
  "password": any,
  "realName": any,
  "age": any,
  "sex": any,
  "birthDay": any,
  "userLevel": any,
  "phone": any,
  "role": any,
  "status": any,
  "imagePath": any,
  "createTime": any,
  "modifyTime": any,
  "lastActiveTime": any,
  "deleted": any,
  "wxOpenId": any
}
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      console.log(ruleForm);
      login<ApiResponsres>(ruleForm).then((res) => {
        console.log(res);
        // console.log(res.response.age);
        if (res.message === '成功') {
          ElMessage({
            message: '登陆成功.',
            type: 'success',
          })
          router.push({ path: "/indexs" })
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

</script>
